import { realpathSync } from 'fs'
import minidev from 'minidev'
import { createLogger } from '../logger'

const cwd = process.cwd()
const projectPath = realpathSync(cwd)

const logger = createLogger('info', { prefix: '[Uploader]' })

export const run = async(appId: string) => {
  const privateKey = await minidev.config.get("alipay.authentication.privateKey")
  const toolId = await minidev.config.get("alipay.authentication.toolId")

  // @ts-ignore
  await minidev.config.useRuntime({
    'alipay.authentication.privateKey': privateKey,
    'alipay.authentication.toolId': toolId,
  })

  logger.info('小程序工具授权成功')

  // @ts-ignore
  await minidev.upload({
    appId,
    clientType: 'alipay',
    experience: true,
    project: projectPath
  })
}
